import api from '../../../utils/api';
// import pipe from '../../../assets/js/pipe.js';
var roleId = localStorage.getItem("_roleId");

export default {
  data(){
    return{
      checked: 'runInfo',
      //某些按钮显示或隐藏
      hiddenInfo:true,
      hiddenInfo1:[],
      // 获取row的key值
      getRowKeys(row) {
        return row.id;
      },
      // 要展开的行，数组的元素是row的key值
      expands: [],
      //弹出框
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      formLabelWidth: '60px',
      // 任务清单列表 查询日期
      form: {
        dateSearch:''
      },
      //任务清单
      taskList:[],
      //雷达 广东省
      list:[],
      list1:[],
      list2:[], 
      list3:{},   
      // 通知公告
      info:[],
      //查看报告
      report:'',
      //页码
      pages:1,
     
      //添加工作计划
      addData:{
        marineBoard:[],
        radarList:[],
        serviceWay:'',
        planDate:'',
        content:''
      },
      select:'',
      select1:'',
      selectList:[],
      page2:1,
      totalPlanNum:0,
      isDisabled2:true,
      isDisabled3:false,

      //任务实施计划
      workData:[],
      totalPlan:0,
      isDisabled4:true,
      isDisabled5:false,
      page3:1,

      //发表公告
      information:{
        persons:[],//人员
        title:'',//标题
        isPublic:'yes',//是否公开
        content:''//内容
      },
      infoSelect:'',//选中人员
      person:"",
      yesOrNo:'yes',//是否公开
      page1:1,
      totalInfo:0,
      //分页按钮是否禁用
      isDisabled:true,
      isDisabled1:false,
    }
  },
  beforeMount(){
    this.forway();
  },
  mounted() {
    //当前雷达运行情况
    this.radarList();
    // 通知公告
    this.getInfo();
    //任务清单列表
    this.maintainPlan();
    //任务实施列表
    this.workPlan();
    this.getFile();
  },
  methods:{
    //时间格式
    format:function(time){
      var t = new Date(time);
      var year = t.getFullYear();
      var month = (t.getMonth()+1);
      var day = t.getDate();
      var hour = t.getHours();
      var minute = t.getMinutes();
      var second = t.getSeconds();
      if(month<10){
          month = "0"+month;
      }
      if(day<10){
          day = "0"+day;
      }
      if(hour<10){
        hour = "0"+hour;
      }
       if(minute<10){
        minute = "0"+minute;
      }
       if(second<10){
        second = "0"+second;
      }
      if(hour =="00" && minute == "00" && second == "00"){
        return t = year+"-"+month+"-"+day;
      }else{
        return t = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
      }
      
    },
    handleToggleDrawer() {
      this.$refs.drawerLayout.toggle();
    },
    toIndex(){
      this.$router.push('/index');
    },
    // 消息通知
    getMessage(){
      this.$router.push('/message');
    },
    // 修改密码
    getPassword(){
      this.$router.push('/modifyPassword');
    },
    //退出   到登陆页面
    forback(){
      this.$router.push('/login');
    },
    //隐藏某些按钮
    forway(){
       // jsgchs
      if( roleId == 'jsgchs'){
        this.hiddenInfo = false;
      }
    },

    //当前雷达运行情况
    async radarList(){
      let data={params:""};
      this.$axios._post(api.radarList, data).then(res=>{
          if (res.data.success) {
            this.list =  res.data.data;
            this.list1 = res.data.data[0].children;
            for(var i=0;i<this.list1.length;i++){
              this.list2.push( this.list1[i].children );
            }
            this.expands.push(this.list[0].id);
            this.changeText(this.list);
            this.changeText(this.list1);
            this.changeText1();
        }
      });
    },
    //批量转换0,1==>正常，故障
    changeText(arr){
      for(var item in arr){
        if(arr[item].problem == '1'){
          arr[item].problem = '正常'
        }
        if(arr[item].problem == '0'){
          arr[item].problem = '故障'
        }
      }
      
    },
    //批量转换0,1==>正常，故障
    changeText1(){
      for(var i in this.list2){
        for(var k in this.list2[i]){ 
          if(this.list2[i][k].problem == '1'){
            this.list2[i][k].problem = '正常'
          }else{
            this.list2[i][k].problem = '故障'
          }
        } 
      }
      
    },
    //查看报告
    async watchReport(i){
      //i==>rows
      //报告title
      if(i.task_type == 'XUN_JIAN'){
        this.report = '巡检报告';
      }else if(i.task_type == 'WEI_XIU'){
        this.report = '维修报告';
      }else{
        this.report = '维护报告';
      }
      var that = this;
      let data = {
        //任务task_sn task_type
        params:"task_sn="+i.task_sn+"&task_type="+i.task_type
      }
      this.$axios._post(api.getReportByTsn,data).then(res=>{
        // window.console.log(res);
        that.dialogFormVisible1 = true;
        if(res.data.success){
          if(res.data.code == 1){
              that.list3 = res.data.data;
          }
          if(res.data.code == -1){
            that.list3={
              reportCode:'暂无数据',
              servicEngineer:'暂无数据',
              taskDescript:'暂无数据'
            }
          }
        }
      }).catch(err=>{
        err.message = '暂无报告';
         that.$message.warning(err && err.message);
      }).finally(()=>{
       
      })
    },




    //通知公告
    async getInfo(){
      let data={
        params:"loginname=陈楚楚&page="+this.page1+"&rows="+3
      }
      this.$axios._post(api.getInfo, data).then(res=>{
        if (res.data.success) {
          this.info = res.data.data;
          this.totalInfo = res.data.total;
          // window.console.log(res);
        }
      })
    },
    //点击添加公告
    addInformation(){
      this.dialogFormVisible2 = true;
      this.getPerson();
    },
    //获取人员数据
    async getPerson(){
      let data={params:""};
      this.$axios._post(api.getPersonAll,data).then(res=>{
        if(res.data.success){
          this.information.persons = res.data.data;
        }
      });
    
    },
    //保存公告
    async saveInformation(){
      this.dialogFormVisible2 = false;
      var that = this;
      let data = {
        params:"user_id="+this.person+"&title="+this.information.title+"&content="+this.information.content+"&isPublic="+this.yesOrNo
      }
      this.$axios._post(api.addNotice,data).then(res=>{
        if(res.data.success){
          that.$message({
            message: '成功发布公告',
            type: 'success'
          });
          that.getInfo();
        }
      }).catch(err=>{
        this.$message.error(err && err.message);
      }).finally(()=>{

      })
     
    },
    //选中人员
    getPersonName(e){
      this.person = e.join(",");
    },
    getYes(e){
      this.yesOrNo = e;
    },
    //点击删除公告
    async deleteInformation(ids){
      var that = this;
      let data = {
        params:"id="+ ids
      }
      this.$axios._post(api.deleteInfo,data).then(res=>{
        if(res.data.success){
          that.$confirm('此操作将删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            that.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(() => {
            that.$message({
              type: 'info',
              message: '已取消删除'
            });          
          });
          that.getInfo();
        }
      });
    
    },
    //公告分页
    prevPages(){
      this.page1--;
      if(this.page1<=1){
          this.isDisabled=true;
      }
      if(this.page1 < Math.ceil(this.totalInfo/3)){
          this.isDisabled1=false;
      }
      this.getInfo();
    },
    nextPages(){
      this.page1++;
        if(this.page1>1){
            this.isDisabled=false;
        }  
        if(this.page1 == Math.ceil(this.totalInfo/3)){
            this.isDisabled1=true;
            this.$message({
                message:"这是最后一页了",
                type:"warning"
            });
        }
        this.getInfo();
    },
    //是否有可以下载的文件
    getFile(){
      for(var i=0;i<this.info.length;i++){
        if(this.info[i].fileurl){
          this.hiddenInfo1.push(false);
        }else{
          this.hiddenInfo1.push(true);
        }
      }
    },
    //点击下载文件
    updown(url){
      window.open(url);
    },


    //任务列表清单
    async maintainPlan() {
      var text = document.getElementsByClassName("text")[0];
      var collapse = document.querySelector(".collapse");
      //检查输入字符串格式
      var str = /^[0-9]{4}-{1}[0-9]{2}$/;
      if(this.form.dateSearch !== ''){
         if(!str.test(this.form.dateSearch)){
          this.$message({
            message: '格式不对，输入内容如2018-07',
            type: 'warning'
          });
          return ;
        }
      }
      var dataSearch = this.form.dateSearch || '';
      let data = {
        params:"month="+dataSearch+"&page="+this.page2+"&rows="+5
      }
      //成功回调的结果
      this.$axios._post(api.maintainPlan, data).then(res=>{
        if (res.data.success) {
          this.totalPlanNum = res.data.total;
          if(res.data.data.length > 0){
            this.taskList = res.data.data;
            // text.innerText="";
            // collapse.style.display="block";
          }
          if(res.data.data.length == 0){
            text.innerHTML="暂无数据";
            collapse.style.display="none";
          }
        }
      });
    
    },
    //下一页
    nextPage(){ 
      this.page2++;
        if(this.page2>1){
            this.isDisabled2=false;
        }  
        if(this.page2 == Math.ceil(this.totalPlanNum/5)){
            this.isDisabled3=true;
            this.$message({
                message:"这是最后一页了",
                type:"warning"
            });
        }
        this.maintainPlan();
    },
    //上一页
    prevPage(){
      this.page2--;
      if(this.page2<=1){
          this.isDisabled2=true;
      }
      if(this.page2 < Math.ceil(this.totalPlanNum/5)){
          this.isDisabled3=false;
      }
      this.maintainPlan();
    },
    //添加工作计划
    addPlan(){
      this.dialogFormVisible3 = true;
      this.addPlandata();
    },
    //工作计划海事局数据
    async addPlandata(){
      var that = this;
      let data={
        params:"orgTypeCode=HAI_SHI"
      }
      this.$axios._post(api.findOrg ,data).then(res=>{
        if(res.data.success){ 
          that.addData.marineBoard = res.data.data;
        }
      });
     
    },
    //选中option
    selectItem(e){
      this.select1='';
      if(e == '湛江海事局'){
        e = 'ZHAN_JIANG_HAISHIJU';
      }else if(e == '广州海事局'){
        e = 'GUANG_ZHOU_HAISHIJU';
      }else if(e == '汕头海事局'){
        e = 'SHAN_TOU_HAISHIJU';
      }else if(e == '茂名海事局'){
        e = 'MAO_MING_HAISHIJU';
      }else if(e == '惠州海事局'){
        e = 'HUI_ZHOU_HAISHIJU';
      }else if(e == '珠海海事局'){
        e = 'ZHU_HAI_HAISHIJU';
      }else{
        e = 'DONG_GUAN_HAISHIJU';
      }
      this.selectRadar(e);
    },
    //工作计划雷达站数据
    async selectRadar(code){
      var that = this;
      let data = {
        params:"orgCode="+code
      }
      this.$axios._post(api.findLeida ,data).then(res=>{
         if(res.data.success){
          that.addData.radarList = res.data.data;
        }
      });
     
    },
    //保存工作计划
    saveInfo(){
      this.saveData();
      this.select="";
      this.select1="";
      this.addData.planDate="";
      this.addData.serviceWay="";
      this.addData.content="";
    },
    //提交工作计划数据
    async saveData(){
       var formatData = (function(date) {
            if(typeof date == 'object'){
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                m = m < 10 ? '0' + m : m;
                var d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                return y + '-' + m + '-' + d;
            }
                return date;    
            })(this.addData.planDate);
      var that = this;
      let data = { 
        //海事局名称  // 雷达站  //	计划时间，yyyy-mm-dd //	任务类型 //	任务内容
        params:"marine_board="+this.select+"&radar_station="+this.select1+"&plandate="+formatData+"&task_type="+this.addData.serviceWay+"&task_content="+this.addData.content
      }
      this.$axios._post(api.addInfo,data).then(res=>{
        if(res.data.success){
          that.$message({
            message:"添加成功",
            type:"success"
          });
          this.dialogFormVisible3 = false;
        }
      });
     
    },
    //删除工作计划
    delect(index){
       this.$confirm('此操作将删除该文件, 是否继续?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delectData(index);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          return false;         
        });
      
    },
    async delectData(index){
      let data={
        params:"id="+ this.taskList[index].id
      }
      this.$axios._post(api.deletePlan, data).then(res=>{
        if (res.data.success){
          this.maintainPlan();
        }
      });
    },


    //任务实施计划
    async workPlan(){
      let data = {
        params:"page="+this.page3+"&rows="+10
      }
      this.$axios._post(api.workPlans, data).then(res=>{
        if (res.data.success){
          this.workData = res.data.data;
          this.totalPlan = res.data.total;
        }
      });
    
    },
    next(){
      this.page3++;
      if(this.page3>1){
          this.isDisabled4=false;
      }  
      if(this.page3 == Math.ceil(this.totalPlan/10)){
          this.isDisabled5=true;
          this.$message({
              message:"这是最后一页了",
              type:"warning"
          });
          return;
      }
      this.workPlan();
    },
    prev(){
      this.page3--;
      if(this.page3<=1){
          this.isDisabled4=true;
      }
      if(this.page3 < Math.ceil(this.totalPlan/10)){
          this.isDisabled5=false;
      }
      this.workPlan();
    },
    next1(page,dis1,dis2,total,fn){
      this[page]++;
      if(this[page]>1){
        this[dis1]=false;
      }  
      if(this[page] == Math.ceil(total/10)){
          this[dis2]=true;
          this.$message({
              message:"这是最后一页了",
              type:"warning"
          });
          return;
      }
      fn();
    },
    next2(){
      this.next1('page3','isDisabled4','isDisabled5',this.totalPlan,this.workPlan);
    }
  }
}